package com.peitamerchants.ui.chat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.peitamerchants.R;
import com.peitamerchants.application.MainApplication;

import java.util.Locale;

import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imlib.model.Conversation;

/**
 * Created by zy on 2017/7/3.
 */

public class ConversationActivity extends AppCompatActivity {
    private Conversation.ConversationType mConversationType;
    private Toolbar toolbar;
    private TextView tv_title;
    private TextView toobal_confirm;
    private String mTargetId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation);
        initView();
        initData();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_title = (TextView) findViewById(R.id.toobal_title);
        toobal_confirm = (TextView) findViewById(R.id.toobal_confirm);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }

    private void initData() {
        Intent intent = getIntent();
         mTargetId = intent.getData().getQueryParameter("targetId");
        String title = intent.getData().getQueryParameter("title").toString();
        String type = intent.getData().getLastPathSegment();// 获得当前会话类型
        mConversationType = Conversation.ConversationType
                .valueOf(intent.getData().getLastPathSegment().toUpperCase(Locale.getDefault()));
        enterFragment(mConversationType, mTargetId);
        if(mConversationType == Conversation.ConversationType.PRIVATE){
            if (!TextUtils.isEmpty(title)) {
                if (title.length() > 6) {
                    title = title.subSequence(0, 6).toString() + "...";
                }
            }
            title = "与" + title + "交谈";
        }

        tv_title.setText(title);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        toobal_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              MainApplication.getCommonPackage().mModule.nativeCallRn(mTargetId);
            }
        });
    }

    @SuppressLint("DefaultLocale")
    public void enterFragment(Conversation.ConversationType mConversationType, String mTargetId) {
        ConversationFragment fragment = (ConversationFragment) getSupportFragmentManager()
                .findFragmentById(R.id.activity_conversation);
        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon().appendPath("conversation")
                .appendPath(mConversationType.getName().toLowerCase()).appendQueryParameter("targetId", mTargetId)
                .build();
        fragment.setUri(uri);
    }
}
